package com.it.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.it.reggie.common.BaseContext;
import com.it.reggie.common.Result;
import com.it.reggie.entity.AddressBook;
import com.it.reggie.service.IAddressBookService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.security.PermitAll;
import java.util.List;

/**
 * @author:cyh
 * @data:2022/12/18 15:16
 */
@Api(tags = "地址管理")
@Slf4j
@RestController
@RequestMapping("/addressBook")
public class addressBookController {
    @Autowired
    IAddressBookService addressBookService;

    /**
     * 查询当前用户的所有地址
     * @return
     */
    @GetMapping("/list")
    public Result<List<AddressBook>> queryAll(){
        LambdaQueryWrapper<AddressBook> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AddressBook::getUserId, BaseContext.getCurrentId());
        List<AddressBook> addressBooks = addressBookService.list(wrapper);
        return Result.success(addressBooks);
    }

    /**
     * 修改默认地址
     * @param addressBook 需要设置成默认地址的id
     * @return
     */
    @Transactional
    @PutMapping("/default")
    public Result updateAddressDefault(@RequestBody AddressBook addressBook){
        LambdaUpdateWrapper<AddressBook> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(AddressBook::getIsDefault,0);
        wrapper.eq(AddressBook::getUserId, BaseContext.getCurrentId());
        //将所有地址的isDefault都修改为0
        addressBookService.update(wrapper);
        //将需要设置成默认地址的isDefault修改为1
        addressBook.setIsDefault(1);
        addressBookService.updateById(addressBook);
        return Result.success("设置成功");
    }

    /**
     * 添加地址
     * @param addressBook 添加的地址信息
     * @return
     */
    @PostMapping
    public Result save(@RequestBody AddressBook addressBook){
        //需要给地址信息设置用户id
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBookService.save(addressBook);
        return Result.success("添加成功");
    }

    /**
     * 根据id查询地址
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<AddressBook> queryById(@PathVariable Long id){
        AddressBook addressBook = addressBookService.getById(id);
        if (addressBook!=null){
            return Result.success(addressBook);
        }else {
            return Result.filed("没有查询到该地址");
        }
    }

    /**
     * 查询当前用户的默认地址信息
     * @return
     */
    @GetMapping("/default")
    public Result<AddressBook> queryDefault(){
        LambdaQueryWrapper<AddressBook> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AddressBook::getUserId, BaseContext.getCurrentId());
        wrapper.eq(AddressBook::getIsDefault, 1);
        AddressBook addressBook = addressBookService.getOne(wrapper);
        if (addressBook!=null) {
            return Result.success(addressBook);
        } else {
            return Result.filed("没有查询到该地址");
        }
    }

    /**
     * 删除一个/多个地址
     * @param ids
     * @return
     */
    @DeleteMapping
    public Result remove(@RequestParam("ids") List<Long> ids){
        addressBookService.removeByIds(ids);
        return Result.success("删除成功");
    }


    @PutMapping
    public Result update(@RequestBody AddressBook addressBook){
        addressBookService.updateById(addressBook);
        return Result.success("修改成功");
    }

}
